package com.mvc.web.common.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;

public class OSSManageUtil {
	 /** 
     * 上传OSS服务器文件 
    * @Title: uploadFile  
    * @Description:  
    * @param @param ossConfigure 
    * @param @param file 
    * @param @return 
    * @param @throws Exception    设定文件  
    * @return String    返回类型  
    * @throws 
     */  
    public static String uploadFile(File file) throws Exception{  
        InputStream fileContent=null;  
        fileContent=new FileInputStream(file);  
          
        OSSClient ossClient=new OSSClient(OSSConfigure.getEndpoint(), OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
 
        //创建上传Object的Metadata  
        ObjectMetadata objectMetadata=new ObjectMetadata();  
        objectMetadata.setContentLength(fileContent.available());  
        objectMetadata.setCacheControl("no-cache");  
        objectMetadata.setHeader("Pragma", "no-cache");  
        objectMetadata.setContentType(contentType(file.getName().substring(file.getName().lastIndexOf(".")+1)));  
        objectMetadata.setContentDisposition("inline;filename=" + file.getName());  
        //上传文件  
        ossClient.putObject(OSSConfigure.getBucketName(),   file.getName(), fileContent, objectMetadata);  
        System.out.println(file.getName());  
        return OSSConfigure.getAccessUrl()+"/" + file.getName();  
    }  
    
    public static String uploadLoanFile(File file) throws Exception{  
    	InputStream fileContent=null;  
    	fileContent=new FileInputStream(file);  
    	
    	OSSClient ossClient=new OSSClient(OSSConfigure.getEndpoint(), OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
    	
    	//创建上传Object的Metadata  
    	ObjectMetadata objectMetadata=new ObjectMetadata();  
    	objectMetadata.setContentLength(fileContent.available());  
    	objectMetadata.setCacheControl("no-cache");  
    	objectMetadata.setHeader("Pragma", "no-cache");  
    	objectMetadata.setContentType(contentType(file.getName().substring(file.getName().lastIndexOf(".")+1)));  
    	objectMetadata.setContentDisposition("inline;filename=" + file.getName());  
    	//上传文件  
    	ossClient.putObject("loan-file",   file.getName(), fileContent, objectMetadata);  
    	System.out.println(file.getName());  
    	return "http://loan-file.oss-cn-beijing.aliyuncs.com/" + file.getName();  
    }  
    public static String CopyObject(String key,String value){  
    	OSSClient ossClient=new OSSClient(OSSConfigure.getEndpoint(), OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
    	//创建上传Object的Metadata  
    	ObjectMetadata objectMetadata=new ObjectMetadata();  
    	objectMetadata.setCacheControl("no-cache");  
    	objectMetadata.setHeader("Pragma", "no-cache");  
    	objectMetadata.setContentType(contentType(value.substring(value.lastIndexOf(".")+1)));  
    	objectMetadata.setContentDisposition("attachment;filename=" + value);  
    	//上传文件  
    	CopyObjectRequest copy = new CopyObjectRequest("loan-file", key, "loan-file", key);
    	copy.setNewObjectMetadata(objectMetadata);
    	CopyObjectResult res = ossClient.copyObject(copy);
    	System.out.println(res.toString());  
    	return res.toString();  
    }  
    public static String uploadFile(InputStream fileContent,String filename) throws Exception{  
        
          
        OSSClient ossClient=new OSSClient(OSSConfigure.getEndpoint(), OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
 
        //创建上传Object的Metadata  
        ObjectMetadata objectMetadata=new ObjectMetadata();  
        objectMetadata.setContentLength(fileContent.available());  
        objectMetadata.setCacheControl("no-cache");  
        objectMetadata.setHeader("Pragma", "no-cache");  
        objectMetadata.setContentType(contentType(filename.substring(filename.lastIndexOf(".")+1)));  
        objectMetadata.setContentDisposition("inline;filename=" + filename);  
        //上传文件  
        ossClient.putObject(OSSConfigure.getBucketName(), filename, fileContent, objectMetadata);  
        System.out.println(filename);  
        fileContent=null;
        return OSSConfigure.getAccessUrl()+"/" + filename;  
    }    
  
    /** 
     * 根据key删除OSS服务器上的文件 
    * @Title: deleteFile  
    * @Description:  
    * @param @param ossConfigure 
    * @param @param filePath    设定文件  
    * @return void    返回类型  
    * @throws 
     */  
    public static void deleteFile(String filename){  
        OSSClient ossClient = new OSSClient(OSSConfigure.getEndpoint(),OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
        ossClient.deleteObject(OSSConfigure.getBucketName(), filename);  
          
    }  
      
     /** 
     * Description: 判断OSS服务文件上传时文件的contentType 
     * @Version1.0 
     * @param FilenameExtension 文件后缀 
     * @return String  
     */  
     public static String contentType(String FilenameExtension){  
        if(FilenameExtension.equals("BMP")||FilenameExtension.equals("bmp")){return "image/bmp";}  
        if(FilenameExtension.equals("GIF")||FilenameExtension.equals("gif")){return "image/gif";}  
        if(FilenameExtension.equals("JPEG")||FilenameExtension.equals("jpeg")||  
           FilenameExtension.equals("JPG")||FilenameExtension.equals("jpg")||     
           FilenameExtension.equals("PNG")||FilenameExtension.equals("png")){return "image/jpeg";}  
        if(FilenameExtension.equals("HTML")||FilenameExtension.equals("html")){return "text/html";}  
        if(FilenameExtension.equals("TXT")||FilenameExtension.equals("txt")){return "text/plain";}  
        if(FilenameExtension.equals("VSD")||FilenameExtension.equals("vsd")){return "application/vnd.visio";}  
        if(FilenameExtension.equals("PPTX")||FilenameExtension.equals("pptx")||  
            FilenameExtension.equals("PPT")||FilenameExtension.equals("ppt")){return "application/vnd.ms-powerpoint";}  
        if(FilenameExtension.equals("DOCX")||FilenameExtension.equals("docx")||  
            FilenameExtension.equals("DOC")||FilenameExtension.equals("doc")){return "application/msword";}  
        if(FilenameExtension.equals("XML")||FilenameExtension.equals("xml")){return "text/xml";}  
        return "text/html";  
     }  
     
     public static String downloadFile(String filename,String downpath){
     	OSSClient ossClient=new OSSClient(OSSConfigure.getEndpoint(), OSSConfigure.getAccessKeyId(), OSSConfigure.getAccessKeySecret());  
     	 
     	GetObjectRequest getObjectRequest = new GetObjectRequest(OSSConfigure.getBucketName(), filename);
 		ObjectMetadata objectMetadata = ossClient.getObject(getObjectRequest, new File(downpath));
 		return objectMetadata.getETag();
     }
     
     public static void  getObjectTofilePath(InputStream is,String path) throws Exception{
		 FileOutputStream os = new FileOutputStream(path);
		 byte[] b = new byte[1024];
         int len = 0;
         while((len=is.read(b))!=-1){
            os.write(b,0,len);
         }
         os.close();
         is.close();
	}
     
     public static void main(String[] args) throws Exception {
	    URL url = new URL("");
        URLConnection uc = url.openConnection();
        String fileName = uc.getHeaderField(6);
        fileName = URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
        System.out.println("文件名为："+fileName);
        System.out.println("文件大小："+(uc.getContentLength()/1024)+"KB");
        String path = "D:"+File.separator+fileName;
        FileOutputStream os = new FileOutputStream(path);
        InputStream is = uc.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while((len=is.read(b))!=-1){
            os.write(b,0,len);
        }
        os.close();
        is.close();
        System.out.println("下载成功,文件保存在："+path);
	}
}
